 package handling.channel.handler;

 import client.MapleCharacter;
 import client.MapleClient;
 import client.inventory.Item;
 import client.inventory.ItemLoader;
 import client.inventory.MapleInventoryType;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import configs.ServerConfig;
 import constants.ItemConstants;
 import database.DatabaseConnection;
 import handling.world.World;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.MapleInventoryManipulator;
 import server.MapleItemInformationProvider;
 import server.MerchItemPackage;
 import server.shops.HiredMerchant;
 import server.shops.IMaplePlayerShop;
 import tools.DateUtil;
 import tools.MaplePacketCreator;
 import tools.Pair;
 import tools.data.input.LittleEndianAccessor;
 import tools.packet.PlayerShopPacket;

 public class HiredMerchantHandler
 {
/*  34 */   private static final Logger log = LogManager.getLogger(HiredMerchantHandler.class.getName());

   public static boolean UseHiredMerchant(MapleClient c, boolean packet) {
/*  37 */     MapleCharacter chr = c.getPlayer();
/*  38 */     if (c.getChannelServer().isShutdown()) {
/*  39 */       chr.dropMessage(1, "服务器即将关闭维护，暂时无法进行开店。");
/*  40 */       return false;
     }
/*  42 */     if (chr.getMap() != null && chr.getMap().allowPersonalShop()) {
/*  43 */       HiredMerchant merchant = World.getMerchant(chr.getAccountID(), chr.getId());
/*  44 */       if (merchant != null) {
/*  45 */         c.announce(PlayerShopPacket.sendTitleBox(8, merchant.getMapId(), merchant.getChannel() - 1));
       } else {

/*  48 */         if (loadItemFrom_Database(chr) == null) {
/*  49 */           if (packet) {
/*  50 */             c.announce(PlayerShopPacket.sendTitleBox(7));
           }
/*  52 */           return true;
         }
/*  54 */         c.announce(PlayerShopPacket.sendTitleBox(9));
       }
     }

/*  58 */     return false;
   }

   public static int getMerchMesos(MapleCharacter chr) {
/*  62 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  63 */       PreparedStatement ps = con.prepareStatement("SELECT * from hiredmerch where characterid = ?");
/*  64 */       ps.setInt(1, chr.getId());
/*  65 */       ResultSet rs = ps.executeQuery();
/*  66 */       if (!rs.next()) {
/*  67 */         ps.close();
/*  68 */         rs.close();
/*  69 */         return 0;
       }
/*  71 */       int mesos = rs.getInt("Mesos");
/*  72 */       rs.close();
/*  73 */       ps.close();
/*  74 */       return (mesos > 0) ? mesos : 0;
/*  75 */     } catch (SQLException se) {
/*  76 */       return 0;
     }  } public static void MerchantItemStore(LittleEndianAccessor slea, MapleClient c) { HiredMerchant merchant;
     boolean merch;
     MerchItemPackage pack;
     int checkstatus;
/*  81 */     MapleCharacter chr = c.getPlayer();
/*  82 */     if (chr == null) {
       return;
     }
/*  85 */     if (c.getChannelServer().isShutdown()) {
/*  86 */       chr.dropMessage(1, "服务器即将关闭维护，暂时无法进行道具取回。");
/*  87 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  90 */     byte operation = slea.readByte();
/*  91 */     switch (operation) {
       case 23:
/*  93 */         merchant = World.getMerchant(chr.getAccountID(), chr.getId());
/*  94 */         if (merchant != null) {
/*  95 */           c.announce(PlayerShopPacket.merchItemStore(merchant.getMapId(), merchant.getChannel() - 1));
/*  96 */           chr.setConversation(0);
         } else {
/*  98 */           MerchItemPackage merchItemPackage = loadItemFrom_Database(chr);

/* 100 */           if (merchItemPackage == null) {
/* 101 */             c.announce(PlayerShopPacket.merchItemStore(999999999, 0));
/* 102 */             chr.setConversation(0);
           } else {
/* 104 */             c.announce(PlayerShopPacket.merchItemStore_ItemData(merchItemPackage));
           }
         }
         return;
       case 24:
/* 109 */         if (chr.getConversation() != 3) {
           return;
         }
/* 112 */         c.announce(PlayerShopPacket.merchItemStore((byte)45));
         return;
       case 31:
/* 115 */         if (chr.getConversation() != 3) {
           return;
         }
/* 118 */         merch = World.hasMerchant(chr.getAccountID(), chr.getId());
/* 119 */         if (merch) {
/* 120 */           chr.dropMessage(1, "请关闭现有的商店.");
/* 121 */           chr.setConversation(0);
           return;
         }
/* 124 */         pack = loadItemFrom_Database(chr);
/* 125 */         if (pack == null) {
/* 126 */           chr.dropMessage(1, "发生了未知错误.");
           return;
         }
/* 129 */         checkstatus = check(chr, pack);
/* 130 */         switch (checkstatus) {
           case 1:
/* 132 */             c.announce(PlayerShopPacket.merchItem_Message((byte)37));
             return;
           case 2:
/* 135 */             c.announce(PlayerShopPacket.merchItem_Message((byte)40));
             return;
           case 3:
/* 138 */             c.announce(PlayerShopPacket.merchItem_Message((byte)38));
             return;
         }
/* 141 */         if (pack.getMesos() > 0L && chr.getMeso() + pack.getMesos() > ServerConfig.CHANNEL_PLAYER_MAXMESO) {
/* 142 */           c.announce(PlayerShopPacket.merchItem_Message((byte)37));
           return;
         }
/* 145 */         if (deletePackage(chr.getId())) {
/* 146 */           if (pack.getMesos() > 0L) {
/* 147 */             chr.gainMeso(pack.getMesos(), false);
/* 148 */             log.info("[雇佣] " + chr.getName() + " 雇佣取回获得金币: " + pack.getMesos() + " 时间: " + DateUtil.getCurrentDate());
/* 149 */             HiredMerchant.log.info(chr.getName() + " 雇佣取回获得金币: " + pack.getMesos());
           }
/* 151 */           for (Item item : pack.getItems()) {
/* 152 */             MapleInventoryManipulator.addFromDrop(c, item, false);
/* 153 */             HiredMerchant.log.info(chr.getName() + " 雇佣取回获得道具: " + item.getItemId() + " - " + MapleItemInformationProvider.getInstance().getName(item.getItemId()) + " 数量: " + item.getQuantity());
           }
/* 155 */           c.announce(PlayerShopPacket.merchItem_Message((byte)36));
         } else {
/* 157 */           chr.dropMessage(1, "发生了未知错误.");
         }
         return;
       case 33:
/* 161 */         chr.setConversation(0);
         return;
     }
/* 164 */     System.out.println("弗洛兰德：未知的操作类型 " + operation); }




   public static void RemoteStore(LittleEndianAccessor slea, MapleClient c) {
/* 170 */     MapleCharacter chr = c.getPlayer();
/* 171 */     HiredMerchant merchant = World.getMerchant(chr.getAccountID(), chr.getId());
/* 172 */     if (merchant != null) {
/* 173 */       if (merchant.getChannel() == chr.getClient().getChannel()) {
/* 174 */         merchant.setOpen(false);
/* 175 */         merchant.removeAllVisitors(20, 0);
/* 176 */         chr.setPlayerShop((IMaplePlayerShop)merchant);
/* 177 */         c.announce(PlayerShopPacket.getHiredMerch(chr, merchant, false));
       } else {
/* 179 */         c.announce(PlayerShopPacket.sendTitleBox(16, 0, merchant.getChannel() - 1));
       }
     } else {
/* 182 */       chr.dropMessage(1, "你没有开设商店");
     }
/* 184 */     c.announce(MaplePacketCreator.enableActions());
   }

   private static int check(MapleCharacter chr, MerchItemPackage pack) {
/* 188 */     if (chr.getMeso() + pack.getMesos() < 0L) {
/* 189 */       log.info("[雇佣] " + chr.getName() + " 雇佣取回道具金币检测错误 时间: " + DateUtil.getCurrentDate());
/* 190 */       HiredMerchant.log.error(chr.getName() + " 雇佣取回道具金币检测错误");
/* 191 */       return 1;
     }
/* 193 */     byte eq = 0, use = 0, setup = 0, etc = 0, cash = 0;
/* 194 */     for (Item item : pack.getItems()) {
/* 195 */       MapleInventoryType invtype = ItemConstants.getInventoryType(item.getItemId());
/* 196 */       if (invtype == MapleInventoryType.EQUIP) {
/* 197 */         eq = (byte)(eq + 1);
/* 198 */       } else if (invtype == MapleInventoryType.USE) {
/* 199 */         use = (byte)(use + 1);
/* 200 */       } else if (invtype == MapleInventoryType.SETUP) {
/* 201 */         setup = (byte)(setup + 1);
/* 202 */       } else if (invtype == MapleInventoryType.ETC) {
/* 203 */         etc = (byte)(etc + 1);
/* 204 */       } else if (invtype == MapleInventoryType.CASH) {
/* 205 */         cash = (byte)(cash + 1);
       }
/* 207 */       if (MapleItemInformationProvider.getInstance().isPickupRestricted(item.getItemId()) && chr.haveItem(item.getItemId(), 1)) {
/* 208 */         log.info("[雇佣] " + chr.getName() + " 雇佣取回道具是否可以捡取错误 时间: " + DateUtil.getCurrentDate());
/* 209 */         HiredMerchant.log.error(chr.getName() + " 雇佣取回道具是否可以捡取错误");
/* 210 */         return 3;
       }
     }
/* 213 */     if (chr.getInventory(MapleInventoryType.EQUIP).getNumFreeSlot() < eq || chr.getInventory(MapleInventoryType.USE).getNumFreeSlot() < use || chr.getInventory(MapleInventoryType.SETUP).getNumFreeSlot() < setup || chr.getInventory(MapleInventoryType.ETC).getNumFreeSlot() < etc || chr.getInventory(MapleInventoryType.CASH).getNumFreeSlot() < cash) {
/* 214 */       log.info("[雇佣] " + chr.getName() + " 雇佣取回道具背包空间不够 时间: " + DateUtil.getCurrentDate());
/* 215 */       HiredMerchant.log.error(chr.getName() + " 雇佣取回道具背包空间不够");
/* 216 */       return 2;
     }
/* 218 */     return 0;
   }

   private static boolean deletePackage(int charId) {
/* 222 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 223 */       PreparedStatement ps = con.prepareStatement("DELETE from hiredmerch where characterid = ?");
/* 224 */       ps.setInt(1, charId);
/* 225 */       ps.executeUpdate();
/* 226 */       ps.close();
/* 227 */       ItemLoader.雇佣道具.saveItems(con, null, charId);
/* 228 */       return true;
/* 229 */     } catch (SQLException e) {
/* 230 */       System.out.println("删除弗洛兰德道具信息出错" + e);
/* 231 */       return false;
     }
   }

   private static MerchItemPackage loadItemFrom_Database(MapleCharacter chr) {
     try {
/* 237 */       long mesos = chr.getMerchantMeso();
/* 238 */       Map<Long, Pair<Item, MapleInventoryType>> items = ItemLoader.雇佣道具.loadItems(false, chr.getId());
/* 239 */       if (mesos == 0L && items.isEmpty())
       {
/* 241 */         return null;
       }
/* 243 */       MerchItemPackage pack = new MerchItemPackage();
/* 244 */       pack.setMesos(mesos);
/* 245 */       if (!items.isEmpty()) {
/* 246 */         List<Item> iters = new ArrayList<>();
/* 247 */         for (Pair<Item, MapleInventoryType> z : items.values()) {
/* 248 */           iters.add(z.left);
         }
/* 250 */         pack.setItems(iters);
       }
/* 252 */       HiredMerchant.log.error(chr.getName() + " 弗洛兰德取回最后返回 金币: " + mesos + " 道具数量: " + items.size());
/* 253 */       return pack;
/* 254 */     } catch (SQLException e) {
/* 255 */       System.out.println("加载弗洛兰德道具信息出错" + e);
/* 256 */       return null;
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\HiredMerchantHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */